(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('cda.popup.localController', localController);
	localController.$inject = [ '$scope', '$q', '$uibModalInstance', 'Constants', 'dialogsModule', 'gridService', 'modalService', 'layoutConfig', 'validatorConfig', 'authService','reportService' ];
	function localController($scope, $q, $uibModalInstance, Constants, dialogsModule, gridService, modalService, layoutConfig, validatorConfig, authService, reportService) {
		var ctrl = this, log = $scope.loggerFactory.getLogger("localLookup");
		ctrl.dialogsModule = dialogsModule;
		ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		
		ctrl.ok = function() {
			if (!dialogsModule.btnCallback || !dialogsModule.btnCallback['ok'] || dialogsModule.btnCallback['ok'](ctrl.selectedRows) !== false) {
				$uibModalInstance.close();
			}
		};
		
		ctrl.close = function() {
			if (!dialogsModule.btnCallback || !dialogsModule.btnCallback['close'] || dialogsModule.btnCallback['close']() !== false) {
				$uibModalInstance.dismiss();
			}
		};
		
		ctrl.init = function() {
			ctrl.reportLocal = {};
			ctrl.innerGridOptions = gridService.createInnerGridOptions($scope, ctrl.layoutConfig.formConfigs.fieldCfg.tableConfigs.table, rowSelectionChanged);
			ctrl.innerGridOptions.data = 'vm.reportLocal.list';
			if(ctrl.dialogsModule.params.multiSelect == "true"){
				ctrl.innerGridOptions.multiSelect = true;
			}
			authService.search(ctrl.reportLocal).then(function(response) {
				ctrl.reportLocal.list = response.data || [];
				if(ctrl.dialogsModule.params.filterLocalList){
					for( var i=ctrl.dialogsModule.params.filterLocalList.length-1; i>=0; i--){
						ctrl.reportLocal.list.splice(ctrl.dialogsModule.params.filterLocalList[i],1);
					}
				}
			});
		};

		function rowSelectionChanged(eventRow, selectedRows) {
			ctrl.selectedRows = selectedRows;
		}
		
	}
})(angular);
